package com.laicunba.ext.web.controller.h5.activity;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.laicunba.ext.web.controller.SuiCunBaoBaseController;
import com.laicunba.pojo.ActivityDO;
import com.laicunba.pojo.ActivityJoinDO;
import com.laicunba.pojo.UserDO;
import com.laicunba.service.ActivityJoinService;
import com.laicunba.service.ActivityService;
import com.laicunba.service.activity.ActivityDataQueryService;
import com.laicunba.service.exception.MyException;
import com.laicunba.util.StringUtil;

@Controller
@RequestMapping(value = "/h5/ext/activity")
public class ActivityController extends SuiCunBaoBaseController {
	private Map<String, ActivityDataQueryService> serviceMap = new HashMap<String, ActivityDataQueryService>();
	@Autowired
	private ActivityJoinService activityJoinService;
	@Autowired
	private ActivityService activityService;

	@Autowired
	public void setActivityDataQueryServiceList(ActivityDataQueryService[] services) {
		for (ActivityDataQueryService service : services) {
			serviceMap.put(service.getDataCode(), service);
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ResponseBody
	@RequestMapping(value = "/join.htm")
	public String partake(String actId, String subActId, HttpServletRequest request) {
		JSONObject json = new JSONObject();
		UserDO userDO = getCurrentUser(request);
		if (userDO == null) {
			json.put("code", "token_error");
			return json.toJSONString();
		}

		JSONObject param = new JSONObject(); 
		Iterator it = request.getParameterMap().keySet().iterator();
		while (it.hasNext()) {
			String key = (String) it.next();
			param.put(key, request.getParameter(key));
		}
		
		try {
			ActivityJoinDO joinDO = 	activityJoinService.doJoinActivity(userDO.getId(), actId, subActId,param.toJSONString());
			json.put("code", joinDO!=null ? "SUCCESS" : "FAIL");
			json.put("join_id", joinDO.getId());
			json.put("join_type", joinDO.getRewardType());
			json.put("join_resp", joinDO.getRewardResp());
		} catch (MyException e) {
			json.put("code", e.getCode());
		}

		return json.toJSONString();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ResponseBody
	@RequestMapping(value = "/data_query.htm")
	public String dataQuery(String activityId, String dataCode, HttpServletRequest request) {
		Map map = new HashMap();
		Map param = new HashMap();

		map.put("ACT_INFO", activityService.get(ActivityDO.class, activityId));

		Iterator it = request.getParameterMap().keySet().iterator();
		while (it.hasNext()) {
			String key = (String) it.next();
			param.put(key, request.getParameter(key));
		}
		UserDO userDO = getCurrentUser(request);
		map.put("USER_LOGIN", userDO!=null);
		param.put("uid", userDO!=null?userDO.getId():null); 
		param.put("activity_id", activityId);
		if (StringUtil.isNotEmpty(dataCode)) {
			dataCode = dataCode.toUpperCase();
			for (String code : dataCode.split(",")) {
				code = code.trim();
				if (!serviceMap.containsKey(code)) {
					continue;
				}
				map.put(code, serviceMap.get(code).query(userDO!=null?userDO.getId():null, param));
			}
		}
		return toJsonObject(map);
	}
}
